import Emitter from '../libs/tinyemitter';
import { SCREEN_WIDTH, SCREEN_HEIGHT } from "../render";

export default class Player extends Emitter {
  constructor() {
    super()
    // 初始化事件监听
    this.initEvent();
  }

  /**
   * 判断手指是否在卡片上
   * @param {Number} x: 手指的X轴坐标
   * @param {Number} y: 手指的Y轴坐标
   * @return {Boolean}: 用于标识手指是否在卡片上的布尔值
   */
  checkIsFingerOnAir(x, y) {
    var arr = GameGlobal.databus.cardsData;
    for (let index = arr.length - 1; index >= 0; index--) {
      const item = arr[index];
      const width = GameGlobal.card.width
      const height = GameGlobal.card.height
      const left = item.left
      const top = item.top
      if (
        x >= left &&
        x <= left + width &&
        y >= top &&
        y <= top + height
      ) {
        return {
          cardData: item,
          index,
        };
      }
    }
  }

  /**
   * 玩家响应手指的触摸事件
   * 改变战机的位置
   */
  initEvent() {

    wx.onTouchStart((e) => {
      let { clientX: x, clientY: y } = e.touches[0];
      x = GameGlobal.px2dpr(x)
      y = GameGlobal.px2dpr(y)
      console.log(x)
      if (GameGlobal.databus.isGameOver) {
        return;
      }

      const info = this.checkIsFingerOnAir(x, y);

      this.emit("clcikCard", info);
    });
  }

  update() {
    if (GameGlobal.databus.isGameOver) {
      return;
    }

    // // 每20帧让玩家射击一次
    // if (GameGlobal.databus.frame % PLAYER_SHOOT_INTERVAL === 0) {
    //   this.shoot(); // 玩家射击
    // }
  }

  destroy() {
    // this.isActive = false;
    // this.playAnimation();
    // GameGlobal.musicManager.playExplosion(); // 播放爆炸音效
    // wx.vibrateShort({
    //   type: "medium",
    // }); // 震动
  }
}
